<template>
	<view class="recharge-manage">
		<view class="card-box">
			<view class="card" v-for="(item,index) in rechargeConfigArr" :key="index">
				<view>{{`充${item.money}送${item.giftMoney}元`}}</view>
				<view class="btn-box">
					<view class="edit-btn" @click="openPop('edit',item)">编辑</view>
					<view class="delete-btn" @click="deleteEvent(item)">删除</view>
				</view>
			</view>
		</view>
		<view class="noMore">暂无更多</view>
		<view class="footer-box">
			<text class="configer-btn" @click="openPop('config')">充值说明</text>
			<text class="add-btn" @click="openPop('add')">添加充值</text>
		</view>

		<uni-popup ref="popup" type="bottom" :safe-area='false'>
			<view class="pop-box" v-if="type !=='config'">
				<view class="pop-title">充值管理</view>
				<view class="pop-item">
					<view>充值金额：</view>
					<uni-easyinput class="pop-input" type="number" :maxlength="10" primaryColor="#ffd589" trim="all"
						v-model="currentRechargeConfig.money" placeholder="请输入"></uni-easyinput>
					<view class="pop-unit">元</view>
				</view>
				<view class="pop-item">
					<view>赠送金额：</view>
					<uni-easyinput class="pop-input" type="number" :maxlength="10" primaryColor="#ffd589" trim="all"
						v-model="currentRechargeConfig.giftMoney" placeholder="请输入"></uni-easyinput>
					<view class="pop-unit">元</view>
				</view>
				<view class="pop-btn-box">
					<view class="cancel-btn" @click="closePop">取消</view>
					<view class="submit-btn" @click="submitEvent">确认</view>
				</view>
			</view>
			<view class="pop-box" v-else> 
				<view class="pop-title">充值说明</view>
				<view class="pop-item">
					<uni-easyinput class="pop-input" type="textarea" :maxlength="300" primaryColor="#ffd589"
						v-model="configDesc" placeholder="请输入充值说明"></uni-easyinput>
				</view>
				<view class="pop-btn-box">
					<view class="cancel-btn" @click="closePop">取消</view>
					<view class="submit-btn" @click="saveConfig">确认</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				shopId: '',
				rechargeConfigArr: [],
				currentRechargeConfig: {
					shopId: '',
					rechargeConfigId: '',
					money: '',
					giftMoney: '',
				},
				type: '',
				configDesc: ''
			};
		},
		methods: {
			// 打开弹窗
			openPop(type, item) {
				this.type = type
				if (type === 'edit') {
					this.currentRechargeConfig = JSON.parse(JSON.stringify(item));
				} else if(type === 'add'){
					this.currentRechargeConfig.shopId = this.shopId;
					this.currentRechargeConfig.rechargeConfigId = '';
					this.currentRechargeConfig.money = '';
					this.currentRechargeConfig.giftMoney = '';
				}
				this.$refs.popup.open('center')
			},
			closePop() {
				this.$refs.popup.close()
			},
			// 添加充值卡
			submitEvent() {
				if (!this.currentRechargeConfig.money) {
					uni.showToast({
						title: '请输入充值金额',
						duration: 2000,
						icon: 'none'
					})
					return
				} else if (!this.currentRechargeConfig.giftMoney) {
					uni.showToast({
						title: '请输入赠送金额',
						duration: 2000,
						icon: 'none'
					})
					return
				}
				if (!this.currentRechargeConfig.rechargeConfigId) {
					const data = {
						shopId: this.currentRechargeConfig.shopId,
						money: this.currentRechargeConfig.money,
						giftMoney: this.currentRechargeConfig.giftMoney
					}
					//保存设备
					this.$request({
						method: 'post',
						url: `/applet/store/member/wallet/rechargeConfig/add`,
						data,
						loading: true,
						isEncrypt: false
					}).then(res => {
						this.requestEvent();
						this.closePop()
					})
				} else {
					const data = this.currentRechargeConfig;
					//保存充值配置
					this.$request({
						method: 'post',
						url: `/applet/store/member/wallet/rechargeConfig/update`,
						data,
						loading: true,
						isEncrypt: false
					}).then(res => {
						this.requestEvent();
						this.closePop()
					})
				}
				this.currentRechargeConfig.shopId = this.shopId;
				this.currentRechargeConfig.rechargeConfigId = ''
				this.currentRechargeConfig.money = ''
				this.currentRechargeConfig.giftMoney = ''
				this.closePop()
			},
			// 删除事件处理
			deleteEvent(item) {
				uni.showModal({
					title: '提示',
					content: '是否确认删除？',
					cancelText: '取消',
					cancelColor: '#666',
					confirmText: '确认',
					confirmColor: '##ff9600',
					success: res => {
						if (res.confirm) {
							this.$request({
								method: 'delete',
								url: `/applet/store/member/wallet/rechargeConfig/${item.rechargeConfigId}`,
								loading: true,
								isEncrypt: false
							}).then(res => {
								this.requestEvent();
							}).catch(err => {
								console.log('删除异常：', err)
							})
						}
					}
				})
			},
			// 请求充值事件处理
			requestEvent() {
				this.getRechargeConfigData();
			},
			// 获取充值说明
			getConfigDesc(){
				this.$request({
					method: 'get',
					url: `/applet/store/member/wallet/rechargeConfigDesc?shopId=${this.shopId}`,
					loading: false,
					isEncrypt: false
				}).then(res => {
					this.configDesc = res.data || ''
				}).catch(err => {
					console.log('查询异常：', err)
				})
			},
			// 保存充值说明
			saveConfig(){
				const data ={
					shopId:this.shopId,
					desc:this.configDesc
				}
				this.$request({
					method: 'post',
					url: `/applet/store/member/wallet/saveRechargeConfigDesc`,
					data,
					loading: false,
					isEncrypt: false
				}).then(res => {
					if (res.code === 200) {
						uni.showToast({
							title: '保存成功',
							duration: 2000,
							icon: 'none'
						})
					} else {
						uni.showToast({
							title: res.msg,
							duration: 2000,
							icon: 'none'
						})
					}
					this.closePop()
				}).catch(err => {
					console.log('查询异常：', err)
				})
			},
			//获取充值金额列表
			getRechargeConfigData() {
				this.$request({
					method: 'get',
					url: `/applet/store/member/wallet/rechargeConfig/list?shopId=${this.shopId}`,
					loading: false,
					isEncrypt: false
				}).then(res => {
					this.rechargeConfigArr = res.data || []
				}).catch(err => {
					console.log('查询异常：', err)
				})
			}
		},
		onLoad(options) {
			this.shopId = options.shopId;
			this.requestEvent()
			this.getConfigDesc()
		}
	}
</script>

<style lang="scss" scoped>
	.recharge-manage {
		padding-bottom: 112rpx;

		.card-box {
			padding: 30rpx;
		}

		.card {
			background: #FFF;
			border-radius: 10rpx;
			padding: 20rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
			box-shadow: 0 2px 6px #CCC;
			margin-bottom: 20rpx;
		}

		.btn-box {
			display: flex;
			height: 120rpx;
			align-items: center;
			font-size: 28rpx;

			.edit-btn {
				padding: 8rpx 16rpx;
				font-size: 24rpx;
				border-radius: 10rpx;
				border: 1px solid #ff9600;
			}

			.delete-btn {
				padding: 8rpx 16rpx;
				font-size: 24rpx;
				border-radius: 10rpx;
				color: #FFF;
				background: #ff9600;
				margin-left: 20rpx;
			}
		}

		.footer-box {
			width: 100%;
			position: fixed;
			display: flex;
			bottom: 0;
			padding: 30rpx;
			text-align: center;
			background: #FFF;
			box-sizing: border-box;

			.configer-btn {
				flex: 1;
				margin-right: 20rpx;
				background: #169bd5;
				color: #FFF;
				padding: 20rpx 40rpx;
				border-radius: 10rpx;
				text-align: center;
			}

			.add-btn {
				flex: 1;
				margin-right: 20rpx;
				background: #ff9600;
				color: #FFF;
				padding: 20rpx 40rpx;
				border-radius: 10rpx;
				text-align: center;
			}
		}

		.pop-box {
			background: #FFF;
			border-radius: 20rpx;
			padding: 30rpx;
			width: 600rpx;

			.pop-title {
				font-size: 32rpx;
				font-weight: 600;
				text-align: center;
				margin-bottom: 30rpx;
			}

			.pop-item {
				display: flex;
				align-items: center;
				font-size: 26rpx;
				margin-bottom: 30rpx;
			}

			.pop-unit {
				margin-left: 10rpx;
			}

			.pop-btn-box {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 20rpx 60rpx;

				.cancel-btn {
					padding: 8rpx 36rpx;
					font-size: 24rpx;
					border-radius: 10rpx;
					border: 1px solid #CCC;
				}

				.submit-btn {
					padding: 8rpx 36rpx;
					font-size: 24rpx;
					border-radius: 10rpx;
					color: #FFF;
					background: #ff9600;
				}
			}
		}

		.noMore {
			text-align: center;
			padding: 32rpx 0;
			color: #CCC;
			font-size: 28rpx;
		}

	}
</style>